<?php

namespace App\Services\Api;

use App\Services\Api\ApiService;

/**
 *
 * return ['status'=>1成功2失败,'msg'=>'提示信息','data'=>'返回数据']
 */
class PushService extends ApiService
{

    #################推送设置####################
    private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
    private $appKey = '';
    private $masterSecret = '';
    private $appId = '';

    public function __construct()
    {
        $this->appKey = config('const.ige_tui_app_key');
        $this->masterSecret = config('const.ige_tui_master_secret');
        $this->appId = config('const.app_id');
    }

    public function push($msgArr = [], $type = 1, $users = [])
    {
        $igt = new \IGeTui($this->host, $this->appKey, $this->masterSecret);
        //设置模板
        // $template = $this->IGtTransmissionTemplateDemo($msgArr);
        $template = $this->IGtNotificationTemplateDemo($msgArr);
        // $template = $this->IGtLinkTemplateDemo($msgArr);
        if ($type == 1) {
            //群推
            $message = new \IGtAppMessage();
            $message->set_isOffline(true); //是否离线
            $message->set_offlineExpireTime(10 * 60 * 1000); //离线时间单位为毫秒，例，两个小时离线为3600*1000*2
            $message->set_data($template);
            $message->set_PushNetWorkType(0); //设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
            $message->set_speed(1000); //设置群推接口的推送速度，单位为条/秒
            $appIdList = array($this->appId);
            $message->set_appIdList($appIdList);
            $res = $igt->pushMessageToApp($message);
        } else {
            //批量推送（别名推送，已在前端注册别名,单次推送数量限制1000以内）
            $message = new \IGtListMessage();
            $message->set_isOffline(true); //是否离线
            $message->set_offlineExpireTime(3600 * 12 * 1000); //离线时间
            $message->set_data($template); //设置推送消息类型
            $message->set_PushNetWorkType(0); //设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送，在wifi条件下能帮用户充分节省流量
            $contentId = $igt->getContentId($message);
            //分批发送
            $count = 0;
            $max = 900; //最大数量
            $targetList = [];
            foreach ($users as $v) {
                if (empty($v)) {
                    continue;
                }
                $target = new \IGtTarget();
                $target->set_appId($this->appId);
                //$target->set_clientId('99236b7219399ea37c52ed7c3c687389');//客户端id推送
                $target->set_alias(strval($v));
                $targetList[] = $target;
                $count++;
                if ($count % $max == 0) {
                    $res = $igt->pushMessageToList($contentId, $targetList);
                    $targetList = [];
                }
            }
            if ($targetList) {
                $res = $igt->pushMessageToList($contentId, $targetList);
            }
        }
    }

    /**
     * 透传功能模板可以兼容android ios
     * @param [type] $title
     * @param [type] $content
     * @method IGtTransmissionTemplateDemo
     * @author chengzhigang<1256699215@qq.com>
     */
    public function IGtTransmissionTemplateDemo($msgArr)
    {
        $template = new \IGtTransmissionTemplate();
        $template->set_appId($this->appId); //应用appid
        $template->set_appkey($this->appKey); //应用appkey
        $template->set_transmissionType(2); //透传消息类型
        $template->set_transmissionContent(json_encode($msgArr)); //透传内容
        $apn = new \IGtAPNPayload();
        $alertmsg = new \DictionaryAlertMsg();
        $alertmsg->body = $msgArr['content'];
        $alertmsg->actionLocKey = "ActionLockey";
        $alertmsg->locKey = "LocKey";
        $alertmsg->locArgs = array("locargs");
        $alertmsg->launchImage = "launchimage";
//        iOS8.2 支持
        $alertmsg->title = $msgArr['title'];
        $alertmsg->titleLocKey = "TitleLocKey";
        $alertmsg->titleLocArgs = array("TitleLocArg");
        $apn->alertMsg = $alertmsg;
        $apn->badge = 1;
        $apn->add_customMsg("payload", $msgArr['id']);
        $apn->contentAvailable = 1;
        $apn->category = "ACTIONABLE";
        $template->set_apnInfo($apn);
        return $template;
    }

    /**
     * 通知透传功能模板 支持android
     * @method
     * @author chengzhigang<1256699215@qq.com>
     * @param
     * @return
     */
    public function IGtNotificationTemplateDemo($msgArr)
    {
        $template = new \IGtNotificationTemplate();
        $template->set_appId($this->appId); //应用appid
        $template->set_appkey($this->appKey); //应用appkey
        $template->set_transmissionType(1); //透传消息类型
        $template->set_transmissionContent($msgArr['content']); //透传内容
        $template->set_title($msgArr['title']); //通知栏标题
        $template->set_text($msgArr['content']); //通知栏内容
        $template->set_logoURL($msgArr['logo']); //通知栏logo
        $template->set_isRing(true); //是否响铃
        $template->set_isVibrate(true); //是否震动
        $template->set_isClearable(true); //通知栏是否可清除
        return $template;
    }

    /**
     * 通知打开链接功能模板
     * @method IGtLinkTemplateDemo
     * @author chengzhigang<1256699215@qq.com>
     * @param
     * @return
     */
    public function IGtLinkTemplateDemo($msgArr)
    {
        $template = new \IGtLinkTemplate();
        $template->set_appId($this->appId); //应用appid
        $template->set_appkey($this->appKey); //应用appkey
        $template->set_title($msgArr['title']); //通知栏标题
        $template->set_text($msgArr['content']); //通知栏内容
        $template->set_logoURL($msgArr['logo']); //通知栏logo
        $template->set_isRing(true); //是否响铃
        $template->set_isVibrate(true); //是否震动
        $template->set_isClearable(true); //通知栏是否可清除
        $template->set_url(""); //打开连接地址
        //iOS推送需要设置的pushInfo字段
        $apn = new \IGtAPNPayload();
        $alertmsg = new \DictionaryAlertMsg();
        $alertmsg->body = $msgArr['content'];
        $alertmsg->actionLocKey = "ActionLockey";
        $alertmsg->locKey = "LocKey";
        $alertmsg->locArgs = array("locargs");
        $alertmsg->launchImage = "launchimage";
//        iOS8.2 支持
        $alertmsg->title = $msgArr['title'];
        $alertmsg->titleLocKey = "TitleLocKey";
        $alertmsg->titleLocArgs = array("TitleLocArg");
        $apn->alertMsg = $alertmsg;
        $apn->badge = 1;
        $apn->add_customMsg("payload", $msgArr['id']);
        $apn->contentAvailable = 1;
        $apn->category = "ACTIONABLE";
        $template->set_apnInfo($apn);
        return $template;
    }

}
